<template>
  <div class="container">
    <div class="panel">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form ref="form" :model="formData" :rules="formRules">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="formData.password"></el-input>
        </el-form-item>
        <el-form-item class="btn_box">
          <el-button type="primary" @click="login()">登录</el-button>
          <el-button type="info" @click="reset()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      formData: {
        username: 'admin',
        password: '123456'
      },
      formRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 ~ 6 个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 ~ 15 个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async login() {
      // this.$refs.form.validate(valid => {
      //   console.log(valid, 'valid')
      // })
      // this.$refs.form
      //   .validate()
      //   .catch(err => {
      //     console.log(err, 'errr...')
      //   })
      //   .then(valid => {
      //     console.log(valid, 'valid...')
      //   })
      const valid = await this.$refs.form.validate().catch(err => err)
      if (!valid) return
      // 发送ajax请求
      // this.$http.post('login', { username: this.formData.username, password: this.formData.password })
      const { data: res } = await this.$http.post('/login', this.formData)
      console.log(res, 'res')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // if (res.meta.status !== 200) {
      //   alert(res.meta.msg)
      //   return
      // }
      sessionStorage.setItem('token', res.data.token)
      this.$router.push('/home')
    },
    reset() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  background-color: #2b4b6b;
  height: 100%;
  .panel {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 1px solid #eee;
      padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      box-shadow: 0 0 10px 0 #ddd;
      img {
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .el-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 20px;
      box-sizing: border-box;
      .btn_box {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
